#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum = 0;
        int n = 0;
        for (auto e : nums) {
            n += e;
        }
        sum = n - x;
        if (sum < 0) {
            return -1;
        }
        n = 0;
        int left = 0;
        int right = 0;
        int size = nums.size();
        int max = -1;
        while (right < size) {
            n += nums[right++];
            while (n > sum) {
                n -= nums[left++];
            }
            if (n == sum) {
                if (max < right - left) {
                    max = right - left;
                }
            }
        }
        if (max == -1) {
            return -1;
        }
        else {
            return size - max;
        }
    }
};